package com.huajx.mina.tcpserver.codec;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

public class LdProtocolCodecFactory implements ProtocolCodecFactory {
	static final byte CONTENT_CLOSING_TAG = 64;
	private final LdEncoderAdapter encoder;
	private final LdDecoderAdapter decoder;
	private final Charset codePage;
	private final boolean isServer;
	private final boolean isPassiveEncryption;

	@Override
	public ProtocolDecoder getDecoder(IoSession arg0) throws Exception {
		return this.decoder;
	}

	@Override
	public ProtocolEncoder getEncoder(IoSession arg0) throws Exception {
		return this.encoder;
	}

	public LdProtocolCodecFactory(Charset charset, boolean isServer, boolean isPassiveEncryption) {
		this.codePage = charset;
		this.isServer = isServer;
		this.isPassiveEncryption = isPassiveEncryption;

		this.encoder = new LdEncoderAdapter(this.codePage, isServer, isPassiveEncryption);
		this.decoder = new LdDecoderAdapter(this.codePage, isServer, isPassiveEncryption);
	}

	public LdProtocolCodecFactory(String charset, boolean isServer, boolean isPassiveEncryption) {
		this(Charset.forName(charset), isServer, isPassiveEncryption);
	}

	public static void setEncoderKey(IoSession session, String key) {
		LdEncoderAdapter.setSecretKey(session, key);
	}

	public static void setDecoderKey(IoSession session, String key) {
		LdDecoderAdapter.setSecretKey(session, key);
	}

	public static String getEncoderKey(IoSession session) {
		return LdEncoderAdapter.getSecretKey(session);
	}

	public static String getDecoderKey(IoSession session) {
		return LdDecoderAdapter.getSecretKey(session);
	}

	public Charset getCharset() {
		return this.codePage;
	}

	public boolean getIsServer() {
		return this.isServer;
	}

	public boolean getIsPassiveEncryption() {
		return this.isPassiveEncryption;
	}
}
